package com.hjy.cabinet;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.hjy.cabinet.db.AlarmLogDbHelper;
import com.hjy.cabinet.db.AlarmLogInstance;
import com.hjy.cabinet.db.BaseConfigHelper;
import com.hjy.cabinet.db.BaseConfigInstance;
import com.hjy.cabinet.db.CabinetInfoHelper;
import com.hjy.cabinet.db.CabinetInfoInstance;
import com.hjy.cabinet.db.OperationLogDBHelper;
import com.hjy.cabinet.db.OperationLogInstance;
import com.hjy.cabinet.db.UserbaseHelper;
import com.hjy.cabinet.db.UserDataInstance;
import com.hjy.cabinet.ws.config.AnnotationScanner;
import com.hjy.cabinet.ws.controller.RequestTestController;
import com.hjy.cabinet.ws.domain.ws.WbsocketResponseBody;
import com.hjy.cabinet.ws.domain.ws.WebSocketRequest;
import com.hjy.cabinet.ws.domain.ws.WsData;
import com.hjy.cabinet.ws.service.NetworkHander;
import com.hjy.cabinet.ws.service.WebsocketBasis;
import com.hjy.cabinet.ws.service.WebsocketService;


public class WebsocketActivity extends AppCompatActivity {
    private Button openButton;
    private Button closeButton;
    private TextView resultTextView;
    private TextView resultTextView2;
    private EditText inputEditText;

    private Button connectButton;
    private TextView statusTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_websocket);
        AnnotationScanner scanner = new AnnotationScanner();
        scanner.scan();
        UserbaseHelper databaseHelper = new UserbaseHelper(this);
        UserDataInstance instance = UserDataInstance.getInstance();

        instance.setDbHelper(databaseHelper);
        OperationLogDBHelper operationLogDBHelper = new OperationLogDBHelper(this);
        OperationLogInstance operationLogInstance = OperationLogInstance.getInstance();
        operationLogInstance.setInstance(operationLogDBHelper);

        CabinetInfoHelper cabinetInfoHelper = new CabinetInfoHelper(this);
        CabinetInfoInstance cabinetInfoInstance = CabinetInfoInstance.getInstance();
        cabinetInfoInstance.setInstance(cabinetInfoHelper);


        AlarmLogDbHelper alarmLogDbHelper = new AlarmLogDbHelper(this);
        AlarmLogInstance alarmLogInstance = AlarmLogInstance.getInstance();
        alarmLogInstance.setInstance(alarmLogDbHelper);

        BaseConfigHelper baseConfigHelper = new BaseConfigHelper(this);
        BaseConfigInstance baseConfigInstance = BaseConfigInstance.getInstance();
        baseConfigInstance.setInstance(baseConfigHelper);


        new Thread(() -> {
            NetworkHander networkHander = NetworkHander.getInstance();
            networkHander.connect();
            networkHander.startScheduledLogSync();
        }).start();


        try {

            // 初始化按钮和文本视图
            openButton = findViewById(R.id.openButton);
            closeButton = findViewById(R.id.closeButton);
            resultTextView = findViewById(R.id.resultTextView);
            resultTextView2 = findViewById(R.id.resultTextView2);
            inputEditText = findViewById(R.id.inputEditText);

            // 设置打开按钮的点击事件
            openButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    WebSocketRequest webSocketRequest = WebSocketRequest.create("KA", new Object[]{"hello"}, "192.168.1.168");
                    WebsocketService websocketService = WebsocketService.getInstance();
                    WbsocketResponseBody wbsocketResponseBody = websocketService.sendMessageSynchronous(webSocketRequest, 1);

                    resultTextView.setText("开始按钮被点击");
                    resultTextView2.setText("");
                }
            });

            // 设置关闭按钮的点击事件
            closeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RequestTestController requestTestController = new RequestTestController();
                    WbsocketResponseBody wbsocketResponseBody = requestTestController.synchronousRequest("1");
                    System.out.println(wbsocketResponseBody);

//
//                    if ("".equals(text)) {
//                        requestTestController.aSynchronousRequest("1");
//                    } else {
//                        requestTestController.aSynchronousRequest(text.toString());
//
//                    }
//

                }
            });


            connectButton = findViewById(R.id.connectButton);
            connectButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        String ip = "192.168.1.168";
                        WebsocketBasis websocketBasis = WebsocketBasis.getInstance();
                        websocketBasis.connectWebSocket(ip);
                        WsData.webSocket = websocketBasis.getWebSocket();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            statusTextView = findViewById(R.id.statusTextView);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        WebsocketBasis websocketBasis = WebsocketBasis.getInstance();
        websocketBasis.close();

    }


}